package com.hemerasolutions.v2trainer.controller;

import javax.faces.bean.ManagedBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.controller.interfaces.BaseBeanInterface;
import com.hemerasolutions.v2trainer.dao.interfaces.ThemeDaoInterface;
import com.hemerasolutions.v2trainer.model.others.Theme;
import com.hemerasolutions.v2trainer.model.users.Academy;
import com.hemerasolutions.v2trainer.model.users.Admin;
import com.hemerasolutions.v2trainer.model.users.PersonalTrainer;
import com.hemerasolutions.v2trainer.model.users.Teacher;
import com.hemerasolutions.v2trainer.model.users.User;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;


@Controller
@ManagedBean(name="themeSwitcherBean")
@Scope("session")
public class ThemeSwitcherBean extends BaseBean implements BaseBeanInterface {
	
	private static final long serialVersionUID = 1L;

	private Map<String, String> themes;
    
    private List<Theme> advancedThemes;
    
    @Autowired
    private ThemeDaoInterface daoTheme;
    
    @Autowired
    private UserBean userBean;
    
    private String theme;
   
    public Map<String, String> getThemes() {
        return themes;
    }

    public String getTheme() {

		this.theme = null; 

    	try {
			switch(this.getActiveUserType()){
				case User.USERTYPE_ACADEMY:
					this.theme = this.getActiveActor(Academy.class).getTheme().getName();
					break;
				case User.USERTYPE_ADMIN:
					this.theme = this.getActiveActor(Admin.class).getTheme().getName();
					break;
				case User.USERTYPE_PT:
					this.theme = this.getActiveActor(PersonalTrainer.class).getTheme().getName();
					break;
				case User.USERTYPE_TEACHER:
					this.theme = this.getActiveActor(Teacher.class).getAcademy().getTheme().getName();
					break;
				case User.USERTYPE_ATHLETE:
					this.theme = this.getContextValue("#{userBean.agreementHolder.theme}", Theme.class).getName();
					break;
				default:
					break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	if(theme == null){
    		this.theme = "aristo";
    	}
    	return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }

    @PostConstruct
    public void init() {
        
    	this.advancedThemes = daoTheme.list();
        
    }
    
    public void saveTheme() { 
       this.setTheme(theme);  
    } 
    
    public List<Theme> getAdvancedThemes() {
        return advancedThemes;
    }


}
